﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MissileFunction : FlyItem
{
    public bool m_Trace = false;
    public  GameObject m_Target;
    public float m_HP { get; set; }
    public bool m_IsUpdate = true;
    public float FlyVelocityMax = 1;
    public override void Start()
    {
        m_HP = 30;
    }

    public override void Update()
    {
        base.Update();
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(gameObject))
        {
            return;
        }
        if (Death)
            return;
        if (!m_IsUpdate)
            return;

        //  导弹逐渐加速
        if (m_Weapon != null && FlyVelocity < FlyVelocityMax)
        {
            Missile missile = m_Weapon as Missile;
            if(missile != null)
                FlyVelocity = Mathf.Clamp(FlyVelocity + Time.deltaTime * missile.m_Acceleration, 0, FlyVelocityMax);
            CurFlyVelocity = FlyVelocity;
        }

        transform.rotation = Quaternion.FromToRotation(Vector3.up, FlyDirection);
        var moveOffset = FlyDirection * (FlyVelocity * Time.deltaTime);
        Life -= moveOffset.magnitude;
        transform.position += moveOffset;

        if(m_Trace && m_Target)
        {
            float trunVelocity = 100;
            if(m_Weapon != null)
            {
                Missile missile = m_Weapon as Missile;
                trunVelocity = missile.m_TrunVelocity;
            }

             var targetDefference = m_Target.transform.position - transform.position;
            var d = Vector3.Cross(targetDefference, FlyDirection).z;
            if(d >= 0)
            {
                FlyDirection = Quaternion.AngleAxis(trunVelocity * Time.deltaTime, new Vector3(0, 0, -1)) * FlyDirection;
            }
            else
            {
                FlyDirection = Quaternion.AngleAxis(trunVelocity * Time.deltaTime, new Vector3(0, 0, 1)) * FlyDirection;
            }
        }
    }

    override public void InitParameter(Vector3 position, Vector3 direction, float flyVelocity, float range, float damge, string weaponClassName, Weapon.DamageType dameageType, Ship ship, Weapon weapon)
    {
        base.InitParameter(position, direction, flyVelocity, range, damge, weaponClassName, dameageType, ship, weapon);
        FlyItemManager.Instance.AddFlyItem(this);
        FlyVelocityMax = FlyVelocity;
        FlyVelocity = 0;
    }
    public void FixedUpdate()
    {
        if (m_Trace && !m_Target && null != m_Influence)
        {
            float distance = 1000000000000;
            for (int i = 0; i < ShipManager.Instance.NumShips; ++i)
            {
                var ship = ShipManager.Instance.GetShip(i);
                var shipCom = ship.GetComponent<Ship>();
                if(ship.gameObject.activeSelf && InfluenceMG.get().isHostile(m_Influence, shipCom.m_ShipStruct.Influence))
                {
                    var defference = ship.transform.position - transform.position;
                    if(defference.magnitude < distance)
                    {
                        distance = defference.magnitude;
                        m_Target = ship.gameObject;
                    }
                }
            }
        }
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
    }
    public void Boom(float damage, Weapon.DamageType damageType)
    {
        switch (damageType)
        {
            case Weapon.DamageType.TYPE_EXPLOSION:
                damage *= 2;
                break;
            case Weapon.DamageType.TYPE_KINETIC:
                damage /= 2;
                break;
        }
        m_HP -= damage;

        if (m_HP <= 0)
        {
            OnBoom();
        }
    }
    public override void clean()
    {
        base.clean();
        m_Target = null;
    }

    protected override void OnBoom()
    {
        base.OnBoom();
        FlyItemManager.Instance.RemoveFlyItem(this);
    }
    protected override void OnRangeMax()
    {
        base.OnRangeMax();
        FlyItemManager.Instance.RemoveFlyItem(this);
    }
}
